<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /* 
        栈的数据结构的特点 后进先出
        1.push  入栈
        2.pop   将栈顶的数据出栈
        3.peek  获取栈顶的数据
        4.isEmpty  判断栈是否为空
        5.size  返回栈的长度
        */

        class Stack{
            constructor(){
                this.items = []
            }
            push(value){
                this.items.push(value)
            }
            pop(){
                return this.items.pop()
            }
            peek(){
                return this.items[this.items.length-1]
            }
            isEmpty(){
                return this.items.length==0
            }
            size(){
                return this.items.length
            }
        }
        var arr = new Stack()
        arr.push(2)
        arr.push(4)
        console.log(arr);
        arr.pop()
        console.log(arr);
        console.log(arr.peek()); // 2
        console.log(arr.isEmpty()); //false
    </script>
</body>
</html>